package com.android.hedgehog;

import android.util.Log;

import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input;

public class FingerTracer {
	Graphics graphics;
	boolean[][][] XY;
	boolean[][] checkXY = new boolean[320][480];
	int px, py, nx, ny, xStep, yStep, xCounter, yCounter;
	float xtotdiff, ytotdiff, xStep2, yStep2, counter;
	boolean flag = false;
	boolean flag2 = false;
	boolean crossedOver = false;
	int currentXY;
	int totalXY;
	int lastX;
	int lastY;
	int pawn = 0;
	int a, b;
	int x, y;
	int lastInt = 1;
	boolean right = true;
	boolean down = true;
	Input input;
	RisingConnectionScreen rcs;

	public FingerTracer(Graphics g, int totalBoxes) {
		graphics = g;
		totalXY = totalBoxes;
		XY = new boolean[totalXY][320][480];
		currentXY = 0;
	}

	public void reFresh(int h) {
		for (int i = 0; i < 320; i++) {
			for (int j = 0; j < 480; j++) {
				XY[h][i][j] = false;
			}
		}
	}

	public void line(int x, int y) {

		if (x == 319)
			x = 318;
		if (x == 0)
			x = 1;
		if (y > 478)
			y = 478;
		if (y < 32)
			y = 32;

		if (flag == false) {
			px = x;
			py = y;
			flag = true;
		} else {
			nx = x;
			ny = y;

			xtotdiff = ((float) (nx - px) / (float) Math.abs(ny - py));
			ytotdiff = ((float) (ny - py) / (float) Math.abs(nx - px));

			xStep = px;
			yStep = py;
			xStep2 = 0;
			yStep2 = 0;
			counter = 0;
			xCounter = 1;
			yCounter = 1;

			if (px == nx) {
				if (py < ny) {
					for (yStep = py; yStep < ny; yStep++)
						drawPoint(px, yStep);
				} else {
					for (yStep = ny; yStep < py; yStep++)
						drawPoint(px, yStep);
				}

			} else if (py == ny) {
				if (px < nx) {
					for (xStep = px; xStep < nx; xStep++)
						drawPoint(xStep, py);
				} else {
					for (xStep = nx; xStep < px; xStep++)
						drawPoint(xStep, py);
				}

			} else {

				if (xtotdiff > 0 && ytotdiff < 0) { // Q1
					while ((xStep <= nx && yStep >= ny)) {
						drawPoint(xStep, yStep);

						if (Math.abs(xtotdiff) >= counter)
							xStep2 += 0.2f;

						if (Math.abs(ytotdiff) >= counter)
							yStep2 -= 0.2f;

						if (Math.abs(xStep2) >= xCounter) {
							xStep++;
							xCounter++;
						}
						if (Math.abs(yStep2) >= yCounter) {
							yStep--;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= Math.abs(xtotdiff)
								&& counter >= Math.abs(ytotdiff))
							counter = 0;

					}

				} else if (xtotdiff < 0 && ytotdiff < 0) {// Q2
					while ((xStep >= nx && yStep >= ny)) {

						drawPoint(xStep, yStep);

						if (Math.abs(xtotdiff) >= counter)
							xStep2 -= 0.2f;

						if (Math.abs(ytotdiff) >= counter)
							yStep2 -= 0.2f;

						if (Math.abs(xStep2) >= xCounter) {
							xStep--;
							xCounter++;
						}
						if (Math.abs(yStep2) >= yCounter) {
							yStep--;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= Math.abs(xtotdiff)
								&& counter >= Math.abs(ytotdiff))
							counter = 0;

					}

				} else if (xtotdiff < 0 && ytotdiff > 0) {// Q3
					while ((xStep >= nx && yStep <= ny)) {
						drawPoint(xStep, yStep);

						if (Math.abs(xtotdiff) >= counter)
							xStep2 -= 0.2f;

						if (Math.abs(ytotdiff) >= counter)
							yStep2 += 0.2f;

						if (Math.abs(xStep2) >= xCounter) {
							xStep--;
							xCounter++;
						}
						if (Math.abs(yStep2) >= yCounter) {
							yStep++;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= Math.abs(xtotdiff)
								&& counter >= Math.abs(ytotdiff))
							counter = 0;

					}

				} else { // Q4
					while ((xStep <= nx && yStep <= ny)) {

						drawPoint(xStep, yStep);

						if (xtotdiff >= counter)
							xStep2 += 0.2f;

						if (ytotdiff >= counter)
							yStep2 += 0.2f;

						if (xStep2 >= xCounter) {
							xStep++;
							xCounter++;
						}
						if (yStep2 >= yCounter) {
							yStep++;
							yCounter++;
						}

						counter += 0.2f;

						if (counter >= xtotdiff && counter >= ytotdiff)
							counter = 0;

					}

				}

			}

			px = nx;
			py = ny;

		}// else

	}

	/**
	 * this method is called after traceLine() method to display the traced line
	 * on the screen. The parameters represent the area of the screen that will
	 * display the line.
	 * 
	 * (x + width) <320 AND (y+height) <480
	 * 
	 * 
	 * @param x
	 *            1st point on x-axis
	 * @param y
	 *            1st point on y-axis
	 * @param width
	 *            2nd point on x-axis
	 * @param height
	 *            2nd point on y-axis
	 * @param color
	 */
	public void drawLine(int x, int y, int width, int height, int color) {
		for (int h = 0; h < currentXY + 1; h++) {
			for (int i = x; i < x + width; i++) {
				for (int j = y; j < y + height; j++) {
					if (XY[h][i][j] == true) {
						graphics.drawPixel(i, j, color);
						if(h>0)
							ifCrossed(h,i,j);
					}
				}
			}
		}

	}// drawLine

	public void drawPoint(int x, int y) {
		for (int i = 0; i < 1; i++) {
			for (int j = 0; j < 1; j++) {
				XY[currentXY][x + i][y + j] = true;
			}
		}
	}

	public boolean ifHit(int x, int y, int width, int height) {
		for (int i = x; i < x + width; i++) {
			for (int j = y; j < y + height; j++) {
				if (XY[currentXY][i][j] == true) {
					if (currentXY + 1 < totalXY)
						currentXY++;
					return true;

				}
			}
		}
		return false;
	}
	public void ifCrossed(int h,int i, int j) {
		for (int a = 0; a < h-1 ; a++) {
					if (XY[a][i][j] == true) {
						crossedOver=true;
					}
			}
	}

}
